function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
/**
 * EL表达式的模型表示：数据结构本质上是一个树形结构。
 * 例如一个串行编排(THEN)：
 * (1) EL表达式形式：THEN(a, b, c, d)
 * (2) JSON表示形式：
 * {
    type: ConditionTypeEnum.THEN,
    children: [
      { type: NodeTypeEnum.COMMON, id: 'a' },
      { type: NodeTypeEnum.COMMON, id: 'b' },
      { type: NodeTypeEnum.COMMON, id: 'c' },
      { type: NodeTypeEnum.COMMON, id: 'd' },
    ],
  }
 * (3) 通过ELNode节点模型表示为：
                                          ┌─────────────────┐
                                      ┌──▶│  NodeOperator   │
                                      │   └─────────────────┘
                                      │   ┌─────────────────┐
                                      ├──▶│  NodeOperator   │
  ┌─────────┐    ┌─────────────────┐  │   └─────────────────┘
  │  Chain  │───▶│  ThenOperator   │──┤   ┌─────────────────┐
  └─────────┘    └─────────────────┘  ├──▶│  NodeOperator   │
                                      │   └─────────────────┘
                                      │   ┌─────────────────┐
                                      └──▶│  NodeOperator   │
                                          └─────────────────┘
 */
var ELNode = /*#__PURE__*/function () {
  function ELNode() {
    _classCallCheck(this, ELNode);
    // 节点类型：可以是编排类型，也可以是组件类型
    // 当前节点的子节点：编排类型有子节点，组件类型没有子节点
    _defineProperty(this, "children", void 0);
    // 当前节点的父节点
    _defineProperty(this, "parent", void 0);
    // 判断类节点类型：主要用于SWITCH/IF/FOR/WHILE等编排类型
    _defineProperty(this, "condition", void 0);
    // 组件节点的id
    _defineProperty(this, "id", void 0);
    // 编排节点的属性：可以设置id/tag等等
    _defineProperty(this, "properties", void 0);
    // 当前节点的X6 Cell内容
    _defineProperty(this, "cells", []);
    // 当前节点的X6 Node内容
    _defineProperty(this, "nodes", []);
    // 代理节点
    _defineProperty(this, "proxy", void 0);
    // 当前操作符节点的开始节点
    _defineProperty(this, "startNode", void 0);
    // 当前操作符节点的结束节点
    _defineProperty(this, "endNode", void 0);
    // 是否折叠
    _defineProperty(this, "collapsed", false);
  }
  _createClass(ELNode, [{
    key: "appendChild",
    value: function appendChild(newNode, index) {
      newNode.parent = this;
      if (this.children) {
        // 尝试在父节点中添加新节点
        if (typeof index === 'number') {
          // 1. 如果有索引
          this.children.splice(index, 0, newNode);
          return true;
        }
        if (index) {
          // 2. 如果有目标节点
          var _index = this.children.indexOf(index);
          if (_index !== -1) {
            this.children.splice(_index + 1, 0, newNode);
            return true;
          }
          if (this.condition === index) {
            // 3. 如果是在condition之后追加
            return this.appendChild(newNode, 0);
          }
        }
        // 4. 否则直接插入
        this.children.push(newNode);
        return true;
      }
      return false;
    }

    /**
     * 在后面添加子节点
     * @param newNode 子节点
     * @param index 指定位置：可以是索引，也可以是兄弟节点
     */
  }, {
    key: "prependChild",
    value: function prependChild(newNode, index) {
      newNode.parent = this;
      if (this.children) {
        // 尝试在父节点中添加新节点
        if (typeof index === 'number') {
          // 1. 如果有索引
          this.children.splice(index, 0, newNode);
          return true;
        }
        if (index) {
          // 2. 如果有目标节点
          var _index = this.children.indexOf(index);
          if (_index !== -1) {
            this.children.splice(_index, 0, newNode);
            return true;
          }
          if (this.condition === index) {
            // 3. 如果是在condition之前追加
            return this.prepend(newNode);
          }
        }
        // 4. 否则直接插入
        this.children.splice(0, 0, newNode);
        return true;
      }
      return false;
    }

    /**
     * 删除指定的子节点
     * @param child 子节点
     */
  }, {
    key: "removeChild",
    value: function removeChild(child) {
      if (this.children) {
        var _index2 = this.children.indexOf(child);
        if (_index2 !== -1) {
          this.children.splice(_index2, 1);
          return true;
        }
      }
      if (this.condition && this.condition === child) {
        return this.remove();
      }
      return false;
    }

    /**
     * 在当前节点的前面、插入新节点
     * @param newNode 新节点
     * @returns
     */
  }, {
    key: "prepend",
    value: function prepend(newNode) {
      if (this.parent) {
        if (this.parent.prependChild(newNode, this)) {
          return true;
        }
      } else {
        return this.prependChild(newNode);
      }
      return false;
    }

    /**
     * 在当前节点的后面、插入新节点
     * @param newNode 新节点
     * @returns
     */
  }, {
    key: "append",
    value: function append(newNode) {
      if (this.parent) {
        if (this.parent.appendChild(newNode, this)) {
          return true;
        }
      } else {
        return this.appendChild(newNode);
      }
      return false;
    }

    /**
     * 删除当前节点
     */
  }, {
    key: "remove",
    value: function remove() {
      if (this.parent) {
        return this.parent.removeChild(this);
      }
      return false;
    }

    /**
     * 替换当前节点为新节点
     * @param newNode 新节点
     * @returns
     */
  }, {
    key: "replace",
    value: function replace(newNode) {
      if (this.parent) {
        this.parent.replaceChild(this, newNode);
      }
      return false;
    }

    /**
     * 替换老节点为新节点
     * @param oldNode 老节点
     * @param newNode 新节点
     */
  }, {
    key: "replaceChild",
    value: function replaceChild(oldNode, newNode) {
      newNode.parent = this;
      if (this.children) {
        // 尝试在子节点中查找老节点位置
        var _index3 = this.children.indexOf(oldNode);
        if (_index3 !== -1) {
          this.children.splice(_index3, 1, newNode);
          return true;
        }
      }
      if (this.condition === oldNode) {
        // 3. 如果是在condition之后追加
        this.condition = newNode;
        return true;
      }
      return false;
    }

    /**
     * 转换为X6的图数据格式
     */
  }, {
    key: "selectNodes",
    value:
    /**
     * 获取需要选中的X6 Cell
     */
    function selectNodes() {
      var _this$parent;
      if (((_this$parent = this.parent) === null || _this$parent === void 0 ? void 0 : _this$parent.condition) === this) {
        return this.parent.getNodes();
      }
      return this.getNodes();
    }

    /**
     * 获取当前X6 Node内容
     */
  }, {
    key: "getNodes",
    value: function getNodes() {
      var nodes = _toConsumableArray(this.nodes);
      if (this.condition) {
        nodes = nodes.concat(this.condition.getNodes());
      }
      if (this.children && this.children.length) {
        this.children.forEach(function (child) {
          nodes = nodes.concat(child.getNodes());
        });
      }
      return nodes;
    }

    /**
     * 获取当前X6 Cell内容
     */
  }, {
    key: "getCells",
    value: function getCells() {
      var cells = _toConsumableArray(this.cells);
      if (this.condition) {
        cells = cells.concat(this.condition.getCells());
      }
      if (this.children && this.children.length && !this.collapsed) {
        this.children.forEach(function (child) {
          cells = cells.concat(child.getCells());
        });
      }
      return cells;
    }

    /**
     * 重置当前存储的X6 Cell内容
     */
  }, {
    key: "resetCells",
    value: function resetCells(cells, nodes) {
      this.cells = cells || [];
      this.nodes = nodes || [];
    }

    /**
     * 获取当前节点的开始节点
     */
  }, {
    key: "getStartNode",
    value: function getStartNode() {
      return this.startNode;
    }

    /**
     * 获取当前节点的结束节点
     */
  }, {
    key: "getEndNode",
    value: function getEndNode() {
      return this.collapsed ? this.startNode : this.endNode;
    }

    /**
     * 添加X6 Node相关内容
     * @param node X6 节点
     */
  }, {
    key: "addNode",
    value: function addNode(node) {
      this.nodes.push(node);
      return node;
    }

    /**
     * 获取属性
     * @returns 属性
     */
  }, {
    key: "getProperties",
    value: function getProperties() {
      var properties = this.properties || {};
      Object.keys(properties).filter(function (key) {
        return properties[key] === '' || properties[key] === null;
      }).forEach(function (key) {
        return properties[key] = undefined;
      });
      return properties;
    }

    /**
     * 设置属性
     */
  }, {
    key: "setProperties",
    value: function setProperties(properties) {
      this.properties = properties;
    }

    /**
     * 获取属性的EL表达式
     * @returns 属性的EL表达式
     */
  }, {
    key: "propertiesToEL",
    value: function propertiesToEL() {
      var properties = this.getProperties();
      return Object.keys(properties).filter(function (key) {
        return properties[key] !== undefined;
      }).map(function (key) {
        return ".".concat(key, "(").concat(propertyToString(properties[key]), ")");
      }).join('');
    }

    /**
     * 转换为EL表达式字符串
     */
  }, {
    key: "toJSON",
    value:
    /**
     * 转换为JSON格式
     */
    function toJSON() {
      var type = this.type,
        condition = this.condition,
        children = this.children,
        properties = this.properties,
        id = this.id;
      return Object.assign({
        type: type
      }, condition ? {
        condition: condition.toJSON()
      } : {}, children ? {
        children: children.filter(function (x) {
          return x;
        }).map(function (child) {
          return child.toJSON();
        })
      } : {}, id ? {
        id: id
      } : {}, properties ? {
        properties: this.getProperties()
      } : {});
    }

    /**
     * 当前模型，是否是参数模型的父节点
     * @param model 模型
     * @returns
     */
  }, {
    key: "isParentOf",
    value: function isParentOf(model) {
      var thisModel = this.proxy || this;
      var nextModel = model.proxy || model;
      while (nextModel) {
        if (nextModel.parent === thisModel) {
          return true;
        }
        nextModel = nextModel.parent;
      }
      return false;
    }

    /**
     * 是否折叠
     */
  }, {
    key: "isCollapsed",
    value: function isCollapsed() {
      var _this$parent2;
      if (((_this$parent2 = this.parent) === null || _this$parent2 === void 0 ? void 0 : _this$parent2.condition) === this) {
        return this.parent.isCollapsed();
      }
      return this.collapsed;
    }

    /**
     * 折叠：展开、收起
     */
  }, {
    key: "toggleCollapse",
    value: function toggleCollapse(collapsed) {
      var _this$parent3;
      if (((_this$parent3 = this.parent) === null || _this$parent3 === void 0 ? void 0 : _this$parent3.condition) === this) {
        this.parent.toggleCollapse(collapsed);
      }
      var target = typeof collapsed !== 'boolean' ? !this.collapsed : collapsed;
      this.collapsed = !!target;
    }
  }]);
  return ELNode;
}();
/**
 * EL表达式操作符可以设置的id和tag等等属性。
 */
export { ELNode as default };
function propertyToString(propertyValue) {
  if (typeof propertyValue === 'string') {
    return "\"".concat(propertyValue, "\"");
  }
  if (_typeof(propertyValue) === 'object') {
    return propertyValue.name || propertyValue;
  }
  return propertyValue;
}